package com.leyou.cart.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.cart.config.JwtProperties;
import com.leyou.cart.entity.Cart;
import com.leyou.cart.interceptors.LoginInterceptor;
import com.leyou.common.auth.entity.Payload;
import com.leyou.common.auth.entity.UserInfo;
import com.leyou.common.auth.utils.JwtUtils;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.CookieUtils;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX="ly:cart:uid:";

    public void addCart(Cart cart) {

        UserInfo info = LoginInterceptor.getUser();

        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX+(info.getId().toString()));

        if(ops.hasKey(cart.getSkuId().toString())){
            String skuJson = ops.get(cart.getSkuId().toString());
            Cart storeCart = JsonUtils.nativeRead(skuJson, new TypeReference<Cart>() {});
            storeCart.setNum(storeCart.getNum()+cart.getNum());
            ops.put(cart.getSkuId().toString(),JsonUtils.toString(storeCart));
        }else{
            ops.put(cart.getSkuId().toString(),JsonUtils.toString(cart));
        }



    }

    public List<Cart> listCarts() {

        UserInfo info = LoginInterceptor.getUser();

        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX+(info.getId().toString()));

        List<Cart> carts = new ArrayList<>();
        ops.values().forEach(cartJson->{
            carts.add(JsonUtils.nativeRead(cartJson, new TypeReference<Cart>() {
            }));
        });
        return carts;
    }

    public void updateCart(Long id, Integer num) {
        UserInfo user = LoginInterceptor.getUser();
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX + (user.getId().toString()));
        String cartJson = ops.get(id.toString());
        Cart cart = JsonUtils.nativeRead(cartJson, new TypeReference<Cart>() {
        });
        cart.setNum(num);
        ops.put(id.toString(),JsonUtils.toString(cart));
    }

    public void deleteCart(Long skuId) {
        UserInfo user = LoginInterceptor.getUser();
        if(!redisTemplate.hasKey(KEY_PREFIX+(user.getId().toString()))){
            throw new LyException(ExceptionEnum.CARTS_NOT_FOUND);
        }
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX + (user.getId().toString()));
        if(!ops.hasKey(skuId.toString())){
            return;
        }
        ops.delete(skuId.toString());
    }

    public void mergeCarts(List<Cart> carts) {
        carts.forEach(cart -> {
            addCart(cart);
        });
    }
}
